package com.ylbh.modules.member.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.member.dto.MemberDTO;
import com.ylbh.common.pojo.member.dto.RevenueLogDTO;
import com.ylbh.common.pojo.member.dto.select.RevenueLogSelectDTO;
import com.ylbh.common.pojo.member.dto.update.ShoppingVoucherUpdateDTO;
import com.ylbh.common.pojo.member.vo.MemberVO;
import com.ylbh.common.pojo.member.vo.RevenueLogDetailsVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.R;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.member.service.MemberService;
import com.ylbh.modules.member.service.RevenueLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 *
 * 购物券修改日志
 * @author mgh
 * @date 2019-04-17 10:30:39
 */
@RestController
@RequestMapping("member/revenueLog")
public class RevenueLogController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private RevenueLogService revenueLogService;


    private final long ADMINID  = 33307L;


    /**
     * 购物券修改日志列表
     */
    @RequestMapping("/queryRevenueLogList")
    @RequiresPermissions("member:revenueLog:list")
    public CommonResult<PageUtils> queryRevenueLogList(RevenueLogSelectDTO params){
        PageUtils page = revenueLogService.queryRevenueLogList(params);

        return Ref.ok(page);
    }

    /**
     * 系统管理购物券明细
     */
    @RequestMapping("/queryAdminIntegralLogList")
    @RequiresPermissions("member:revenueLog:queryAdminIntegralLogList")
    public CommonResult<PageUtils> queryAdminIntegralLogList(RevenueLogSelectDTO params){
        PageUtils page = revenueLogService.queryAdminIntegralLogList(params);

        return Ref.ok(page);
    }

    @ApiOperation("用户现在券数量")
    @RequestMapping("queryExistingCoupons/{name}")
    public CommonResult<Integer> queryExistingCoupons(@PathVariable String name){
        Integer coupon = revenueLogService.queryExistingCoupons(name);
        return Ref.ok(coupon);
    }


    /**
     * 修改购物券数量
     */
    @SysLog("修改购物券数量")
    @ApiOperation("修改购物券数量")
    @RequestMapping("/modifyShoppingVoucher")
    @RequiresPermissions("revenueLog:modifyShoppingVoucher")
    public R modifyShoppingVoucher(ShoppingVoucherUpdateDTO params){
        MemberVO userMemberVO = memberService.getByUserName(params.getName());
        if(userMemberVO == null ){
            return R.error("账户错误");
        }
        MemberVO adminMemberVO= memberService.getUserById(ADMINID);

        int integral = params.getIntegral();

        if(integral > adminMemberVO.getIntegral()){
            return R.error("管理账户余额不足");
        }

        //修改用户的购物劵
        MemberDTO userMemberDTO = new MemberDTO();
        userMemberDTO.setIntegral(userMemberVO.getIntegral() + integral);
        userMemberDTO.setId(userMemberVO.getId());
//        memberService.updateById(userMemberDTO);
        memberService.updateUserIntegralById(userMemberDTO);

        //修改管理员的购物劵
        userMemberDTO = new MemberDTO();
        userMemberDTO.setIntegral(adminMemberVO.getIntegral() - integral);
        userMemberDTO.setId(ADMINID);
//        memberService.updateById(userMemberDTO);
        memberService.updateUserIntegralById(userMemberDTO);

        //用户收入记录日志
        RevenueLogDTO revenueLogDTO = new RevenueLogDTO();
        revenueLogDTO.setAddTime(new Date());
        revenueLogDTO.setRemark(params.getRemark());
        revenueLogDTO.setDeleteStatus(false);

        revenueLogDTO.setType(integral > 0 ? 0 : 1);
        revenueLogDTO.setUserId(userMemberVO.getId());
        revenueLogDTO.setFromUserId(ADMINID);
        revenueLogDTO.setFromType(integral > 0 ? 2 : 3);
//        revenueLogDTO.setIntegral(userMemberVO.getIntegral() + integral);
        if (integral < 0) {
            String s = String.valueOf(integral);
            String coupon = s.substring(1, s.length());
            revenueLogDTO.setIntegral(Integer.parseInt(coupon));
        } else {
            revenueLogDTO.setIntegral(integral);
        }
//        revenueLogDTO.setIntegral(integral);
        MemberVO userById = memberService.getUserById(userMemberVO.getId());
        revenueLogDTO.setUserIntegral(userById.getIntegral());
        revenueLogService.add(revenueLogDTO);

        //管理支出记录日志
        revenueLogDTO.setType(integral > 0 ? 1 : 0);
        revenueLogDTO.setUserId(ADMINID);
        revenueLogDTO.setFromUserId(userMemberVO.getId());
        revenueLogDTO.setFromType(integral > 0 ? 3 : 2);
//        revenueLogDTO.setIntegral(userMemberVO.getIntegral() - integral);
//        revenueLogDTO.setIntegral(integral);
        MemberVO userById2 = memberService.getUserById(ADMINID);
        revenueLogDTO.setUserIntegral(userById2.getIntegral());
        revenueLogService.add(revenueLogDTO);

        return R.ok();
    }

    @ApiOperation("购物券明细详情")
    @GetMapping("getRevenueLogDetails/{id}")
    public CommonResult<RevenueLogDetailsVO> getRevenueLogDetails(@PathVariable Long id) {
        RevenueLogDetailsVO vo = revenueLogService.getRevenueLogDetails(id);
        return Ref.ok(vo);
    }

}
